﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Account;
using Volo.Abp.Authorization.Permissions;
using Volo.Abp.Data;
using Volo.Abp.Identity;
using Volo.Abp.Threading;

namespace AbpProjectTemplate.Account
{
    /// <summary>
    /// 当前用户信息
    /// </summary>
    public class ProfileAppService : Volo.Abp.Account.ProfileAppService
    {
        private readonly IPermissionDefinitionManager _permissionDefinitionManager;
        public ProfileAppService(
            IdentityUserManager userManager,
            IOptions<IdentityOptions> identityOptions,
            IPermissionDefinitionManager permissionDefinitionManager) :
            base(userManager, identityOptions)
        {
            _permissionDefinitionManager = permissionDefinitionManager;
        }


        public override async Task<ProfileDto> GetAsync()
        {
            var dto = await base.GetAsync();
            var permissions = (await _permissionDefinitionManager.GetPermissionsAsync())
                 .Where(t => AsyncHelper.RunSync(() => AuthorizationService.IsGrantedAnyAsync(t.Name)))
                 .Select(t => t.Name)
                 .ToArray();
            dto.SetProperty("permissions", permissions);
            return dto;
        }

        //[AllowAnonymous]
        //public async Task<ActionResult> GetAvatarAsync()
        //{
        //    var item = await this._userCache.GetAsync(CurrentUser.GetId());
        //    var blobName = item.GetProperty<string>(AppUserConsts.AvatarExtensionName);
        //    return await _blobContainer.GetFileStream(blobName);
        //}
    }
}
